package com.zhongwang.mms.module.basic.service;

import com.zhongwang.mms.gen.entity.TWarehouseInfo;
import com.zhongwang.mms.gen.entity.TWarehouseInfoExample;
import com.zhongwang.mms.module.basic.dao.TWarehouseInfoDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

/**
 * @Description: 仓库区位 服务层
 * @Author: China.wsg
 * @Date: 2020/04/13 16:26
 */
@Service
public class TWarehouseInfoService {

    @Autowired
    private TWarehouseInfoDao warehouseInfoDao;

    /**
     * 取得仓库区位
     *
     * @param warehouseInfo 检索条件
     * @return
     */
    public List<TWarehouseInfo> findWarehouseInfo(TWarehouseInfo warehouseInfo) {

        TWarehouseInfoExample ex = new TWarehouseInfoExample();
        ex.setOrderByClause("whi_order_no asc");
        TWarehouseInfoExample.Criteria criteria = ex.createCriteria();
        criteria.andWhiStatusNotEqualTo(0);  // 状态：0=删除 1=可用 2=失效

        if (warehouseInfo.getWhiName() != null && !"".equals(warehouseInfo.getWhiName())) {
            criteria.andWhiNameLike("%" + warehouseInfo.getWhiName() + "%");
        }
        if (warehouseInfo.getWhiStatus() != null && !"".equals(warehouseInfo.getWhiStatus())) {
            criteria.andWhiStatusEqualTo(warehouseInfo.getWhiStatus());
        }

        return warehouseInfoDao.selectByExample(ex);
    }

    /**
     * 保存仓库信息
     *
     * @param warehouseInfo 仓库信息
     * @return 影响行数
     */
    @Transactional(rollbackFor = Exception.class)
    public int save(TWarehouseInfo warehouseInfo) {
        int row = warehouseInfoDao.insert(warehouseInfo);
        return row;
    }

    /**
     * 按名称名查询仓库
     *
     * @param name 仓库名
     * @return 仓库信息
     */
    public TWarehouseInfo findWarehouseByName(String name) {
        TWarehouseInfoExample ex = new TWarehouseInfoExample();
        TWarehouseInfoExample.Criteria criteria = ex.createCriteria();
        criteria.andWhiNameEqualTo(name);
        criteria.andWhiStatusNotEqualTo(0);

        List<TWarehouseInfo> warehouseInfos = warehouseInfoDao.selectByExample(ex);
        if (warehouseInfos != null && warehouseInfos.size() > 0) {
            return warehouseInfos.get(0);
        }
        return null;
    }

    /**
     * 修改仓库
     *
     * @param warehouseInfo 仓库信息
     * @return 修改行数
     */
    public String del(TWarehouseInfo warehouseInfo) {

        // 如果被引用 则不允许被删除
        List<TWarehouseInfo> warehouseInfos = warehouseInfoDao.findWarehouseUsedCount(warehouseInfo.getWhiId());
        if (warehouseInfos != null && warehouseInfos.size() > 0) {
            return "该仓库已经被使用，不允许执行删除操作";
        }

        int row = warehouseInfoDao.updateByPrimaryKeySelective(warehouseInfo);

        // 删除下级
        TWarehouseInfo tDept = new TWarehouseInfo();
        tDept.setWhiStatus(0);
        TWarehouseInfoExample ex = new TWarehouseInfoExample();
        TWarehouseInfoExample.Criteria criteria = ex.createCriteria();
        criteria.andWhiPidEqualTo(warehouseInfo.getWhiId());
        warehouseInfoDao.updateByExampleSelective(tDept, ex);

        return "";
    }

    /**
     * 根据主键查询仓库
     *
     * @param id 主键
     * @return 仓库
     */
    public TWarehouseInfo findWarehouseById(Integer id) {
        return warehouseInfoDao.selectByPrimaryKey(id);
    }

    /**
     * 修改仓库
     *
     * @param warehouseInfo 仓库信息
     * @return 影响行数
     */
    @Transactional(rollbackFor = Exception.class)
    public int updateWarehouse(TWarehouseInfo warehouseInfo) {
        int row = warehouseInfoDao.updateByPrimaryKeySelective(warehouseInfo);
        return row;
    }

    /**
     * 根据仓库类型查库位
     * @param type
     * @return
     */
    public List<TWarehouseInfo> findWarByAllocationType(Integer type){
        TWarehouseInfo tWarehouseInfo = new TWarehouseInfo();
        if(type == 1){
            tWarehouseInfo.setWhiType("外购件库");
        }else if(type == 2){
            tWarehouseInfo.setWhiType("原料库");
        }else if(type == 3){
            tWarehouseInfo.setWhiType("成品库");
        }
        return warehouseInfoDao.findWarehouseInfoListByWhiType(tWarehouseInfo.getWhiType());
    }


}
